#!/usr/bin/env python
from wiki_indexes.wiki_index_drug import drug_wiki_index as drugs
import httplib
import time

def get_status_code(host, path="/"):
    """ This function retreives the status code of a website by requesting
        HEAD data from the host. This means that it only requests the headers.
        If the host cannot be reached or something else goes wrong, it returns
        None instead.
    """
    try:
        conn = httplib.HTTPConnection(host)
        conn.request("HEAD", path)
        return conn.getresponse().status
    except StandardError:
        return None


__author__ = 'gogo'

__version__ = '0.0.1'

# print drugs


for drug in drugs:
    url = "http://www.drugs.com/%(drug)s.html" % locals()
    cfile = "/%(drug)s.html" % locals()
    status_code = get_status_code("www.drugs.com", cfile)
    print drug, "||", status_code
    #time.sleep(1)


#check 301 status codes
#
# with open('test_result/300_drugs.txt') as f:
#     for line in f:
#         drug = line.split()[0]
#     #    url = "http://www.drugs.com/%(drug)s.html" % locals()
#         cfile = "/cdi/%(drug)s.html" % locals()
#         status_code = get_status_code("www.drugs.com", cfile)
#         print drug, "||", status_code


# driver = webdriver.Firefox()


